#include "draw.h"

#include "../gl/gl.h"

void flip_draw_init(flip_draw_buffer* self)
{
	self->mode = 0;
	self->textured = 0;
	self->colored = 0;
	self->cur_vert = self->vertices;
	self->cur_tex = self->texcoords;
	self->cur_col = self->colors;
	
	self->colr = 0.f;
	self->colg = 0.f;
	self->colb = 0.f;
}

void flip_draw_clear(flip_draw_buffer* self)
{
	flip_draw_flush(self);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}


void flip_draw_color(flip_draw_buffer* self, float r, float g, float b)
{
	//flip_draw_flush(self);
	
	//glColor3f(r, g, b);
	if(self->colr != r || self->colg != g || self->colb != b)
	{
		flip_draw_set_color(self, 1);
		self->colr = r;
		self->colg = g;
		self->colb = b;
	}
}

void flip_draw_fill_colors(flip_draw_buffer* self)
{
	float* p = self->vertices;
	float* cp = self->colors;
	
	for(; p != self->cur_vert; p += 2)
	{
		*cp++ = self->colr;
		*cp++ = self->colg;
		*cp++ = self->colb;
	}
	
	self->cur_col = cp;
}


void flip_draw_flush(flip_draw_buffer* self)
{
	if(self->cur_vert == self->vertices)
		return; // Nothing to draw
		
	if(self->colored)
		glEnableClientState(GL_COLOR_ARRAY);
	else
		glColor3f(self->colr, self->colg, self->colb);
	glEnableClientState(GL_VERTEX_ARRAY);
	//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glVertexPointer(2, GL_FLOAT, 0, self->vertices);
		if(self->colored)
			glColorPointer(3, GL_FLOAT, 0, self->colors);
		//glTexCoordPointer(2, GL_DOUBLE, 0, texcoords);
		glDrawArrays(GL_QUADS, 0, self->cur_vert - self->vertices);
	if(self->colored)
		glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
	//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	
	self->cur_vert = self->vertices;
	self->cur_col = self->colors;
	self->colored = 0;
}